package com.sunnyatwal.kettlebell.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.sunnyatwal.kettlebell.R;

public abstract class ExerciseUnit implements Parcelable {
	
	private static final int SECONDS_TO_MILLI = 1000;
	
	private int icon;
	private String name;
	private int totalTime;
	private int curTime = 0;
	private boolean requestBeep = false;
		
	protected static final int tempIcon = R.drawable.scroll;
	;

	public static int convert(int minutes, int seconds, boolean inMilliseconds) {
		int time = minutes * 60 + seconds;
		time =  inMilliseconds ? time * SECONDS_TO_MILLI : time;
		return time;
	}
	
	public ExerciseUnit(int icon, String name, int time) {
		this.icon = icon;
		this.name = new String(name);
		this.totalTime = time;
	}
	
	protected ExerciseUnit(Parcel p) {
		icon = p.readInt();
		name = p.readString();
		totalTime = p.readInt();
		curTime = p.readInt();
		requestBeep = p.readByte() == 1;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(icon);
		dest.writeString(name);
		dest.writeInt(totalTime);
		dest.writeInt(curTime);
		dest.writeByte((byte) (requestBeep ? 1 : 0));
	}
	
	public int getIcon() {
		return icon;
	}
	
	public void setIcon(int icon) {
		this.icon = icon;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getTotalTime() {
		return totalTime;
	}
	
	public int getTotalTimeMinutes() {
		return (totalTime / 1000) / 60;
	}
	
	public int getTotalTimeSeconds() {
		return (totalTime / 1000) % 60;
	}
	
	private String getTimePretty(int time)
	{
		int milli = time % 1000;
		int seconds = (time / 1000) % 60;
		int minutes = (time / 1000) / 60;
		
		if (milli > 0)
			seconds++;
		
		String strSeconds = Integer.toString(seconds);
		String strMinutes = Integer.toString(minutes);
		
		strSeconds = strSeconds.length() < 2 ? "0" + strSeconds : strSeconds;
		strMinutes = strMinutes.length() < 2 ? "0" + strMinutes : strMinutes;
		
		return new String(strMinutes + ":" + strSeconds);	
	}
	
	public String getTotalTimePretty(boolean includeTitle) 
	{						
		return "Time: " + getTimePretty(totalTime);		
	}
	
	public String getRemainingTimePretty(boolean includeTitle) 
	{		
		String ret = getTimePretty(totalTime - curTime);
		
		if (includeTitle)
			ret = "Cur: " + ret;
		
		return ret;		
	}
	
	public int getCurrentTime() {
		return curTime;
	}
	
	public void setCurrentTime(int time) {
		this.totalTime = time < 0 ? 0 : time;
	}
	
	public void decrementTime(int d) {
		totalTime = totalTime - d < 0 ? 0 : totalTime - d;
	}
	
	public String getPrettyListMainText() {
		return getName();
	}
	
	public String getPrettyListSubText() {
		return getTotalTimePretty(true);
	}
		
	public String getPrettyNotificationSubText()
	{
		return getRemainingTimePretty(true);
		
	}
	
	public String getPrettyNotificationMainText() {
		return getName();
	}	

	public int getRemainingTime() {
		return totalTime - curTime;
	}
	
	public boolean ElapseTimeBasic(int pCountDownInterval) {
		curTime += pCountDownInterval;	
		
		if(curTime >= totalTime)
		{
			return true;
		}
		return false;
	}
	
	abstract public ElapsedTimeResult ElapseTime(int pCountDownInterval);

	
}